const role = require("../../model/user/role")
const Role = require("../../model/user/role")
const rule = require("../../model/user/rule")
const Rule = require("../../model/user/rule")
const { handleNumber } = require("../../rules/verification")
// 引入外部模块 获取当前时间
const { getTime } = require("../../utils/gettime")
// // 引入 nanoid 生成唯一字符串
// const { nanoid } = require("nanoid")
const Sequelize = require("sequelize")


// 只获取角色名称和 id
exports.getRoleName = async () => {
    let res = await Role.findAll({ attributes: ["id", "rolename"] })
    if (!res) return { msg: "没有您要查找的角色", code: "400" }
    return { msg: "查询成功", code: "200", data: res }
}

// 根据id查询角色
exports.getRoleById = async ({ id }) => {
    // try {
    let res = await Role.findOne({
        attributes: ["id", "rolename", "creator"],
        where: { id },
        include: [{ model: rule }]
    })
    if (!res) return { msg: "没有您要查找的角色", code: "400" }
    return { msg: "查询成功", code: "200", data: res }
    // } catch (err) { return { msg: "查询失败", code: "200", err } }
}

// 查询角色列表
exports.getRoleList = async ({ pageNum, pageSize }) => {
    try {
        if (!pageNum) {
            let res = await Role.findAll({
                attributes: ["id", "rolename"],
                include: [{ model: rule }]
            })
            return { msg: "查询成功", code: "200", data: res }
        } else {
            let checkedNum = handleNumber(pageNum, "pageNum")
            if (checkedNum.code !== "200") return checkedNum
            let checkedSize = handleNumber(pageSize, "pageSize")
            if (checkedSize.code !== "200") return checkedSize
            let res = await Role.findAndCountAll({
                offset: checkedNum.data * checkedSize.data - checkedSize.data,
                limit: checkedSize.data,
                attributes: [ "id", "rolename", "isdelete", "creator", "rolestatus"],
                include: [{
                    model: rule,
                    attributes: ["rulename","id","summary"]
                }],
                distinct:true
            })
            return { msg: "查询成功", code: "200", data: res }
        }
    } catch (err) { return { msg: "查询失败", code: "400", err } }
}

// 添加角色
exports.createRole = async ({ rolename, rolekey, creator }) => {
    try {
        let isCreate = await Role.findOne({ where: { rolename } })
        if (isCreate) return { msg: "添加失败 角色名重复", code: "400" }
        let newData = {}
        if (rolename) newData.rolename = rolename;
        if (rolekey) newData.rolekey = rolekey;
        if (creator) newData.creator = creator;
        newData.createdate = getTime();
        let createRole = await Role.create(newData)
        if (createRole) {
            let getRuleByRolekey = rolekey.split(",")
            let rules = await Rule.findAll({
                where: { rulename: getRuleByRolekey }
            })
            // return getRuleByRolekey
            await createRole.addRules(rules)
            let res = await Role.findOne({
                attributes: ["id", "rolename", "createdate", "creator"],
                where: { rolename },
                include: [{
                    attributes: ["rulename", "summary", "id"],
                    model: Rule
                }]
            })
            return { msg: "添加角色成功", code: "200", data: res }
        } else {
            return { msg: "添加角色失败", code: "400" }
        }
    } catch (err) { return { msg: "添加角色失败", code: "400", err } }
}

// 编辑角色
exports.editRole = async ({ id, rolename, rolekey, rolestatus, isdelete, remark, updator }) => {
    try {
        let newData = {}
        if (rolename) newData.rolename = rolename
        if (rolestatus) newData.rolestatus = rolestatus
        if (isdelete) newData.isdelete = isdelete
        if (remark) newData.remark = remark
        if (updator) newData.updator = updator
        newData.updated = getTime()
        let rolekeyArr = rolekey.split(",")
        let rules = await rule.findAll({
            where: { rulename: rolekeyArr }
        })
        // let isUpdate = await role.update(newData, { where: { id } })
        role.findByPk(id).then((role) => {
            role.update(newData)
            role.setRules(rules)
        })
        // return aaa
        return { msg: "编辑成功", code: "200" }
    } catch (err) { return { msg: "编辑失败", code: "400", err } }
}

// 删除角色
exports.removeRole = async ({ id }) => {
    try {
        let isRemove = await Role.destroy({ where: { id } })
        if (isRemove) return { msg: "删除成功", code: "200" }
        return { msg: "删除失败", code: "400" }
    } catch (err) { return { msg: "删除失败", code: "400", err } }
}